﻿using System;
using System.Collections.Generic;
using Protocol.Constant;
using Protocol.Dto.Fight;

namespace PokerServer.Cache.Fight
{
    /// <summary>
    /// 牌库 一共54牌
    /// </summary>
    public class LibraryModel
    {
        private Queue<CardDto> cardQueue;


        public LibraryModel()
        {
            Init();
        }

        public void Init()
        {
            // 创建牌
            CreateCard();
            // 洗牌
            Shuffle();
        }


        private void CreateCard()
        {
            cardQueue = new Queue<CardDto>();

            int sum = 0;

            for (int color = CardColor.CLUE; color <= CardColor.SQUARE; color++)
            {
                for (int weight = CardWeight.THREE; weight <= CardWeight.TWO; weight++)
                {
                    string cardName = CardColor.GetString(color) + CardWeight.GetString(weight);
                    CardDto cardDto = new CardDto(cardName, color, weight);
                    cardQueue.Enqueue(cardDto);

                    sum++;
                }
            }


            // 创建大小王
            CardDto sJoker = new CardDto("SJoker", CardColor.NONE, CardWeight.SJOKER);
            CardDto lJoker = new CardDto("LJoker", CardColor.NONE, CardWeight.LJOKER);

            cardQueue.Enqueue(sJoker);
            cardQueue.Enqueue(lJoker);

            Console.WriteLine("一共有" + sum + "牌");
            Console.WriteLine(cardQueue.Count);
        }

        /// <summary>
        /// 洗牌的方法
        /// </summary>
        private void Shuffle()
        {
            List<CardDto> newList = new List<CardDto>();


            Random random = new Random();

            foreach (CardDto card in cardQueue)
            {
                int index = random.Next(0, newList.Count + 1);

                newList.Insert(index, card);
            }


            cardQueue.Clear();

            foreach (CardDto dto in newList)
            {
                cardQueue.Enqueue(dto);
            }
        }

        // 发牌
        public CardDto Deal()
        {
            if (cardQueue.Count > 0)
            {
                return cardQueue.Dequeue();
            }

            Console.WriteLine("牌不够了有问题");
            return null;
        }
    }
}